﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Text;

using Fuse8.ContentManagementFramework.Administration.Models;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.Administration.Controllers
{
    public class CmsAdminSitemapController : CmsAdminController
    {
        public ActionResult Index()
        {
            var data = SiteAdminMapService.FindAll();

            CmsAdminEntityTreeNode<CmsSiteAdminMap> nodeTree = null;

            if (data != null)
            {
                var rootNode = data.FirstOrDefault(p => !p.ParentNodeId.HasValue);
                if (rootNode != null)
                {
                    nodeTree = GetTreeNode(rootNode, data);
                }
            }

            var result = new List<CmsAdminEntityTreeNode<CmsSiteAdminMap>>();
            result.Add(nodeTree ?? new CmsAdminEntityTreeNode<CmsSiteAdminMap>());

            return View(result);
        }

        public ActionResult SiteMapDetails(int id)
        {
            ActionResult result = null;

            try
            {
                var node = SiteAdminMapService.FindOne(id);

                result = PartialView(node);
            }
            catch (Exception ex)
            {
                ControllerContext.HttpContext.Response.StatusCode = 500;
                result = Content(ex.Message, "text/html", Encoding.UTF8);
            }

            return result;
        }

        [HttpPost]
        public ActionResult Add(CmsSiteAdminMap model)
        {
            ActionResult result = null;

            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    try
                    {
                        model.IsSpecial = false;
                        model.IsCustom = true;

                        SiteAdminMapService.Add(model);

                        result = Json(1);
                    }
                    catch (Exception ex)
                    {
                        ControllerContext.HttpContext.Response.StatusCode = 500;
                        result = Content(ex.Message, "text/html", Encoding.UTF8);
                    }
                }
                else
                {
                    result = PartialView(model);
                }
            }

            return result ?? PartialView();
        }

        [HttpPost]
        public ActionResult EditOrDelete(CmsSiteAdminMap model)
        {
            ActionResult result = null;

            if (model != null)
            {
                if (ModelState.IsValid)
                {
                    bool isUpdate = !string.IsNullOrEmpty(ControllerContext.HttpContext.Request.Form["btnSave"]);
                    bool isDelete = !string.IsNullOrEmpty(ControllerContext.HttpContext.Request.Form["btnDelete"]);

                    try
                    {
                        model.IsSpecial = false;

                        if (isUpdate && !isDelete)
                        {
                            SiteAdminMapService.Save(model);
                            result = Json(1);
                        }
                        else if (!isUpdate && isDelete)
                        {
                            SiteAdminMapService.Remove(model);
                            result = Json(1);
                        }
                        else
                        {
                            result = Json(0);
                        }                        
                    }
                    catch (Exception ex)
                    {
                        ControllerContext.HttpContext.Response.StatusCode = 500;
                        result = Content(ex.Message, "text/html", Encoding.UTF8);
                    }
                }
                else
                {
                    result = PartialView(model);
                }
            }

            return result ?? PartialView();
        }
    }
}
